package com.example.springmybatis.controller;

import com.example.springmybatis.entity.Group;
import com.example.springmybatis.service.GroupService;
import com.example.springmybatis.vo.GroupUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller
@RestController
@RequestMapping("/group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @GetMapping("/{gid}")
    public Group findGroupById(@PathVariable("gid") Integer gid) {

        System.out.println("findGroupById groupId=>"+gid);
        Group group = groupService.findGroupById(gid);
        System.out.println("findGroupById group=>"+group.toString());
        return group;
    }

    @PostMapping("/add")
    public Group addGroup(@RequestBody Group group) {

        System.out.println("insert group=>"+group.toString());

        return groupService.insertGroup(group);

    }

    @PostMapping("/update")
    public Group updateGroup(@RequestBody Group group) {

        System.out.println("update group=>"+group.toString());

        return groupService.updateGroup(group);
    }

    @GetMapping("/delete/{gid}")
    public void deleteGroup(@PathVariable("gid") Integer gid) {

        System.out.println("delete group=>"+gid);

        groupService.deleteGroup(gid);

    }

    @GetMapping("/")
    public List<Group> findAllGroup() {

        List<Group> groups = groupService.findAllGroup();
        System.out.println("findAllGroup group=>"+groups);
        return groups;
    }

    @GetMapping("/find/{gid}")
    public GroupUserVo findGroupUsersByGid(@PathVariable("gid") Integer gid) {
        GroupUserVo groupUserVo = groupService.findGroupUsersByGid(gid);
        System.out.println("findGroupUsersByGid  =>" + groupUserVo.toString());
        return groupUserVo;
    }

}
